home *** CD-ROM | disk | FTP | other *** search
- Path: chronicle.mti.sgi.com!austern
- From: davew@trigati.cs.haverford.edu (David G. Wonnacott)
- Newsgroups: comp.std.c++,gnu.g++.help
- Subject: Rationale behind disallowal of non-const reference to rvalue
- Followup-To: comp.std.c++
- Date: 28 Mar 1996 09:35:40 PST
- Organization: Bryn Mawr and Haverford College NetNews
- Approved: austern@isolde.mti.sgi.com
- Message-ID: <DAVEW.96Mar27195129@trigati.cs.haverford.edu>
- NNTP-Posting-Host: isolde.mti.sgi.com
- X-Original-Date: 28 Mar 1996 00:51:29 GMT
- X-Auth: PGPMoose V1.1 PGP comp.std.c++
- iQBVAwUBMVrN7Ey4NqrwXLNJAQGEEwIAycfmWzC8dDNJTp821V+aDwzb4GxUoKmw
- KnYvi00Q5Qc5jc4dk+vDU83+iDoi3EA+KTIEj/E+aWYf8vMrjejwUg==
- =w784
- Originator: austern@isolde.mti.sgi.com
-
- [Moderator's note: this article is crossposted to comp.std.c++ and
- gnu.g++.help, and followups have been set to comp.std.c++. Please be
- aware of what newsgroup your article is going to when you respond. mha]
-
- I am looking for an explanation for the rationale behind the decision
- of the standards committee to disallow the binding of a non-const
- reference to an rvalue. I assume this has been discussed to death
- here before, but I haven't been able to find it in a FAQ; I'd be happy
- with a reference to another document rather than a full reply here.
-
- The only answer I've seen for this question before is that it is
- disallowed because the reference may outlive the rvalue it refers to.
- But isn't this just as much a risk for const references as non-const?
- I can't see why non-const references should be illegal when const
- references are legal.
-
- One other question (for the g++ group) - is the current plan for G++
- to eventually give an error, rather than a warning, when this rule is
- broken? If so, is there any possibility of a switch to disable this?
-
-
-
- A detailed explanation of my question, and references, follow. Those
- of you who have seen this before may want to skip the rest.
-
- Paragraph 2 of Section 13.3.3.1.4 of the April draft of the C++
- standard (http://www.cygnus.com/misc/wp/draft/, repeated below for
- convenience) states that the following is illegal (at least as far as
- I understand it):
-
- class foo { ... };
- foo f1();
- int f2(foo &f);
-
- void test()
- {
- int i = f2(f1());
- }
-
- g++ agrees with my reading, giving the following warning for this code:
-
- In function `void test()':
- warning: initialization of non-const `foo &' from rvalue `foo'
- warning: in passing argument 1 of `f2(foo &)'
-
- However, if f2 is changed to "foo f2(const foo &f);", there is no
- problem (either according to my reading of the standard or to g++).
-
- So - my questions are:
-
- 1) What can go wrong with f2 as it is written that can't go wrong with
- f2 if it took a const foo &? I know that the reference could out-live
- the compiler generated temporary - but that can happen with the
- "legal" code as well.
-
- 2) If there is no error that would be prevented by the introduction of
- const, why is the code legal with const and not without? Just because
- nobody thought it would be useful to do this? We do it all over the
- place in the Omega Library (http://www.cs.umd.edu/projects/omega) - we
- want to perform an operation that extracts information from the
- argument of f2, and return the extracted information. Whats the big
- deal if this operation may have a side effect?
-
- Puzzled,
- Dave Wonnacott
- davew@cs.haverford.edu
-
-
- ------- Relevant sections of http://www.cygnus.com/misc/wp/draft/ -------
-
- 13.3.3.1.4 Reference binding [over.ics.ref]
-
- 1 The operation of binding a reference is not a conversion, but for the
- purposes of overload resolution it is considered to be part of a stan-
- dard conversion sequence (specifically, it is the last step in such a
- sequence).
-
- 2 A standard conversion sequence cannot be formed if it requires binding
- a reference to non-const to an rvalue (except when binding an implicit
- object parameter; see the special rules for that case in
- _over.match.funcs_). [Note: this means, for example, that a candidate
- function cannot be a viable function if it has a non-const reference
- parameter (other than the implicit object parameter) and the corre-
- sponding argument is a temporary or would require one to be created to
- initialize the reference (see _dcl.init.ref_). ]
- ---
- [ comp.std.c++ is moderated. To submit articles: Try just posting with your
- newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
- comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
- Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
- Comments? mailto:std-c++-request@ncar.ucar.edu
- ]
-